package net.synck.gcontacts.handlers;

import java.util.Iterator;

import net.synck.gcontacts.wizards.MergeWizard;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.WizardDialog;

import com.google.gdata.data.contacts.ContactEntry;

/**
 * Our sample handler extends AbstractHandler, an IHandler base class.
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class CallMergeWizardHandler extends AbstractHandler {
	/**
	 * The constructor.
	 */
	public CallMergeWizardHandler() {
	}

	/**
	 * the command has been executed, so extract extract the needed information
	 * from the application context.
	 */
	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindow(event);
		MergeWizard wizard = new MergeWizard();
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event)
			.getActivePage().getSelection();
		if (selection != null & selection instanceof IStructuredSelection) {
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			/*
			boolean typeIsOk = true;
			for (Iterator iterator = strucSelection.iterator(); iterator.hasNext();) {
				Object element = (Object) iterator.next();
				if (!(element instanceof ContactEntry)) {
					typeIsOk = false;
					break;
				}
			}
			*/
			//if (typeIsOk) {
				//wizard.setContacts(strucSelection.toArray());
			wizard.setContacts(strucSelection);
				//System.out.println(strucSelection.toArray().length);
			//}
		}
		
		WizardDialog dialog = new WizardDialog(window.getShell(), wizard);
		dialog.open();

		return null;
	}

}
